<template>
    <el-row>
        <el-col :span="4" style="border:1px solid #DCDFE6;height: 100%;">
            <div style="height:30px;padding: 1px 2px 1px 2px;text-align: center;cursor: pointer;margin-bottom:10px;">
                <el-input
                    placeholder="请输入菜单名称/编码"
                    prefix-icon="el-icon-search"
                    size="small"
                    v-model="selectInput"
                    @input="handleInput">
                </el-input>
            </div>
            <div class="treeBody">
                <el-scrollbar wrap-class="scrollbar-wrap" view-class="scrollbar-view">
                    <el-tree :data="selectMenus" ref="tree"
                             node-key="id"
                             default-expand-all
                             :expand-on-click-node="false"
                             highlight-current
                             @node-click="handleTree"
                             style="padding: 1px 5px 5px 10px;"></el-tree>
                </el-scrollbar>
            </div>
        </el-col>
        <el-col :span="20" style="padding-left:20px;height: 100%;">
            <table-view :show-block-control="false">
                <el-form ref="form" :model="form" label-width="80px" :inline="true" slot="formSlot">
                    <el-form-item label="编码" prop="perm">
                        <el-input v-model="form.perm" size="small"></el-input>
                    </el-form-item>
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="form.name" size="small"></el-input>
                    </el-form-item>
                </el-form>

                <div slot="btnBeforeSlot">
                    <el-button type="info" plain icon="el-icon-document-add" size="small" @click="handleCustomAdd">新增
                    </el-button>
                </div>
                <div slot="btnAfterSlot">
                    <el-button type="info" plain icon="el-icon-search" size="small" @click="handleSearch">查询</el-button>
                    <el-button type="info" plain icon="el-icon-s-release" size="small" @click="handleRest">重置
                    </el-button>
                </div>

                <template slot="tableSlot">
                    <el-table v-loading="loading" :data="tableData" stripe height="100%" size="small" row-key="id" width="100%">
                        <el-table-column type="selection" width="55"></el-table-column>
                        <el-table-column prop="code" label="权限编码" width="250"></el-table-column>
                        <el-table-column prop="name" label="权限名称" width="220"></el-table-column>
                        <el-table-column prop="createTime" label="创建时间" width="220"></el-table-column>
                        <el-table-column prop="updateTime" label="更新时间" width="220"></el-table-column>
                        <el-table-column label="操作" fixed="right"  width="220">
                            <template slot-scope="scope">
                                <el-button type="primary" plain size="mini" icon="el-icon-edit" @click="handleCustomAdd(scope.row)"></el-button>
                                <el-button type="danger" plain size="mini" icon="el-icon-delete" @click="handleDel(scope.row)"></el-button>
                                <el-button type="info" plain size="mini" icon="el-icon-search" @click="handleDetail(scope.row)"></el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </template>

                <template slot="otherSlot">
                    <dialog-view ref="dialogRef" :title="dialogParams.title" :width="dialogParams.width">
                        <template slot="dialogSlot">
                            <component :is="dialogParams.componentName" :editData="editRow"
                                       @submitSuccess="handleSearch"></component>
                        </template>
                    </dialog-view>
                </template>
            </table-view>
        </el-col>
    </el-row>
</template>

<script>
    import TableView from '@/components/tableView'
    import DialogView from '@/components/dialogView'
    import tableMixin from '@/mixins/table.mixin'
    import {mapState, mapActions} from 'vuex'
    export default {
        name: "Perm",
        data() {
            return {
                searchApi: this.$api.system.perm.list,
                deleteApi: this.$api.system.menu.delete,
                title: '权限',
                activeSerach: false,
                form: {
                    perm: '',
                    name: ''
                },
                selectInput:'',
                selectMenus: []
            }
        },
        mixins: [tableMixin],
        components: {
            TableView,
            DialogView,
            Add: () => import('./add'),
            Detail: () => import('./detail')
        },
        computed: {
            perms: {
                get() {
                    return this.$store.state.basic.perms
                },
                set(val) {
                    this.$store.state.basic.perms = val;
                }
            },
            ...mapState('basic',['menus'])
        },
        methods: {
            ...mapActions('basic', ['doMenusOfBasic']),
            handleRefreshBtn() {
            },
            handleTree(val) {
                let node = this.$refs.tree.getCurrentNode()
                if(node.leaf == 0) {
                    this.$MessageN.warning("请在叶子菜单节点增加权限");
                }
                this.activeSerach = true;
                this.handleSearch();
            },
            resetSearchParams(search) {
                search.menuId = this.$refs.tree.getCurrentKey();
            },
            handleCustomAdd(row) {
                if (!this.activeSerach || !this.$refs.tree.getCurrentKey()) {
                    this.$MessageN.warning("请先选择菜单");
                    return;
                }
                if (row.constructor.name == 'MouseEvent') {
                    this.handleAdd({enabled: 1, parentId: this.$refs.tree.getCurrentKey()});
                } else {
                    this.handleAdd(row);
                }
            },
            handleInput(val) {
                this.selectMenus =[];
                for(let i = 0;i < this.menus.length;i++) {
                    if(this.menus[i].label.indexOf(val) != -1 || this.menus[i].value.indexOf(val) != -1) {
                        this.selectMenus.push({...this.menus[i]});
                        return;
                    } else {
                        if(this.menus[i].children && this.menus[i].children.length > 0) {
                            this.searchTree(val, this.menus[i].children);
                        }
                    }
                }
            },
            searchTree(val, menus) {
                for(let i = 0;i < menus.length;i++) {
                    if(menus[i].label.indexOf(val) != -1 || menus[i].value.indexOf(val) != -1) {
                        this.selectMenus.push({...menus[i]});
                        return;
                    } else {
                        if(menus[i].children && menus[i].children.length > 0) {
                            this.searchTree(val, menus[i].children);
                        }
                    }
                }
            }
        },
        mounted() {
            this.doMenusOfBasic().then(result=>{
                this.selectMenus = [...this.menus]
            });
        }
    }
</script>

<style scoped lang="less">
    @import "../../../assets/css/common.less";

    .treeTitle {
        background: #f9fafc;
        height: 30px;
        text-align: center;
        line-height: 30px;
    }

    .treeBody {
        min-height: 500px;
        height: 91%;
        padding: 5px 5px;
        margin-bottom: 10px;
        margin-top: -1px;
    }
</style>
